#include "../server/mylib/include/httplib.h"

// g++ -std=c++11 httplibSvr.cc -o http_server -lpthread

// 虚拟机关闭防火墙
//  sudo systemctl stop firewalld
//  sudo systemctl disable firewalld

void Hello(const httplib::Request &req, httplib::Response &rsp)
{
    rsp.set_content("Hello World!", "text/plain"); // text/plain: 纯文本格式
    rsp.status = 200;
}
void Numbers(const httplib::Request &req, httplib::Response &rsp)
{
    auto numbers = req.matches[1]; // 0保存整体path 往后下标中保存捕捉的数据
    rsp.set_content(numbers, "text/plain");
    rsp.status = 200;
}
void Multipart(const httplib::Request &req, httplib::Response &rsp)
{
    auto isFile = req.has_file("file");
    if (!isFile)
    {
        std::cout << "no file upload" << std::endl;
        rsp.status = 400;
        return;
    }
    // const httplib::MultipartFormData &file
    const auto &file = req.get_file_value("file");
    rsp.body.clear();
    rsp.body = file.filename;
    rsp.body += "\n";
    rsp.body += file.content;
    rsp.set_header("Content-Type", "text/plain");
    rsp.status = 200;
    return;
}

int main()
{
    httplib::Server server; // 实例化一个Server类的对象用于搭建服务器
    // 当服务端接收到客户端发来的 请求/hi路径下资源的http请求 就会调用Hello函数

    server.Get("/hi", Hello); // 注册一个针对/hi的Get请求的处理函数映射关系
    server.Get(R"(/numbers/(\d+))", Numbers);
    server.Post("/multipart", Multipart);

    server.listen("0.0.0.0", 8080);
    return 0;
}